package com.xzj.admin.boot.controller;

import com.xzj.admin.boot.domain.vo.SysDeptVO;
import com.xzj.common.id.gen.DefaultIdHelper;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import com.xzj.common.datasource.domain.PageReq;
import cn.hutool.core.util.ObjectUtil;
import com.xzj.common.datasource.domain.PageResp;
import com.xzj.admin.boot.domain.entity.SysDept;
import com.xzj.admin.boot.service.SysDeptService;

import java.util.List;
import java.util.Map;

/**
 * @program: xzj-admin
 * @ClassName: SysDeptController
 * @description: 部门表;(sys_dept)表控制层
 * @author: XiaZunJun
 * @create: 2024-7-30
 */
@Tag(name = "部门表对象功能接口")
@RestController
@RequestMapping("/sysDept")
public class SysDeptController{
    @Resource
    private SysDeptService sysDeptService;

    /**
     * 通过ID查询单条数据
     *
     * @param deptId 主键
     * @return 实例对象
     */
    @Operation(summary = "通过ID查询单条数据")
    @GetMapping("/queryById/{deptId}")
    public SysDept queryById(@PathVariable(name = "deptId")long deptId){
        return sysDeptService.queryById(deptId);
    }

    /**
     * 分页查询
     *
     * @param pageReq 分页筛选条件对象
     * @return 查询结果
     */
    @Operation(summary = "分页查询")
    @PostMapping("/pageQuery")
    public PageResp<SysDept> pageQuery(@RequestBody PageReq<SysDept> pageReq){
        return sysDeptService.pageQuery(pageReq);
    }

    @Operation(summary ="查询组织树")
    @GetMapping("/getDeptList")
    public List<SysDeptVO> getDeptList(SysDept sysDept){
        return sysDeptService.getDeptList(sysDept);
    }

    @Operation(summary ="查询所有组织map")
    @GetMapping("/deptMap")
    public Map<String,String> deptMap(SysDept sysDept){
        return sysDeptService.deptMap(sysDept);
    }


    /**
     * 新增数据
     *
     * @param sysDept 实例对象
     * @return 实例对象
     */
    @Operation(summary ="新增Or更新数据")
    @PostMapping("/addOrUpdate")
    public SysDept addOrUpdate(@RequestBody SysDept sysDept){
        if(ObjectUtil.isNotEmpty(sysDept.getDeptId())){
            sysDeptService.update(sysDept);
        }else{
            sysDept.setDeptId(DefaultIdHelper.nextIdStr());
            sysDeptService.insert(sysDept);
        }
        return sysDept;
    }

    /**
     * 通过主键删除数据
     *
     * @param deptId 主键
     * @return 是否成功
     */
    @Operation(summary ="通过主键删除数据")
    @GetMapping("/deleteById/{deptId}")
    public boolean deleteById(@PathVariable(name = "deptId") long deptId){
        return sysDeptService.deleteById(deptId);
    }



    /**
     * 通过对象删除数据
     *
     * @param sysDept 删除对象
     * @return 是否成功
     */
    @Operation(summary ="通过对象删除数据")
    @PostMapping("/deleteRecord")
    public boolean deleteRecord(@RequestBody SysDept sysDept){
        return sysDeptService.delete(sysDept);
    }
}